<template>
    <div>
        <div class="container">
            <el-container>
                <el-header><el-button type="success" @click="func_addTableData">添加数据行</el-button></el-header>
                <el-main>
                    <el-card class="box-card">
                        <el-table :data="attributeValueList" row-key="id" border class="table" header-cell-class-name="table-header">
                            <el-table-column label="index" width="100" align="center">
                                <template #default="scope">
                                {{scope.$index +1 }}
                                </template>
                            </el-table-column>
                            <el-table-column prop="id" v-if="false" />
                            <el-table-column prop="attrId" v-if="false" />
                            <el-table-column prop="attrValue" label="属性选项">
                                <template #default="scope">
                                    <el-input v-model="scope.row.attrValue" class="handle-input mr10" />
                                </template>
                            </el-table-column>
                            <el-table-column prop="sort" label="排序">
                                <template #default="scope">
                                    <el-input v-model="scope.row.sort" class="handle-input mr10"/>
                                    <!-- <el-input-number v-model="scope.row.sort" :min="1" :max="10" size="small"/> -->
                                </template>
                            </el-table-column>
                            <el-table-column width="160" label="操作">
                                <template #default="scope">
                                    <el-button type="text" class="red" @click="func_handlerDel(scope.$index, scope.row.id)"> <i class="el-icon-delete" />删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-card>
                </el-main>
                <el-footer>
                    <el-button type="primary" @click="func_onSubmit">保存数据</el-button>
                    <el-button @click="func_onClose">关闭</el-button>
                </el-footer>
            </el-container>
        </div>
    </div>
</template>

<script>
import { onMounted, ref, reactive, toRefs, watch } from 'vue';
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter,useRoute } from 'vue-router';
export default {
    name: "attributeValuePop",
  	props:{
		selectValue: Object
	},
	emits: ["attributeValueData"],
    setup(props, cex){
		onMounted(() => {
		});
        const router = useRouter();
		const route = useRoute();
        const formLabelWidth = ref("70px");
        const attributeValueList = ref([]);
		// =========================================================================================
        const func_addTableData = () =>{
            if(attributeValueList == null){
                attributeValueList.value = [];
            }
            attributeValueList.value.push({
                id: 0,
				attrId: 0,
				sort: 1,
				attrValue: "",
            })
        };
        const func_handlerDel = (index ,id) => {
            attributeValueList.value.splice(index, 1);
        };
        const func_onSubmit = () => {
            cex.emit('attributeValueData', {state: 1, data: attributeValueList});
        };
        // 重置
        const func_onClose = () => {
            cex.emit('attributeValueData', {state: 2, data: []});
        };
        watch(() => props.selectValue, (val) => {
            if(attributeValueList == null){
                attributeValueList.value = [];
            }
        },{deep: true, immediate: true});
        return {
            formLabelWidth, attributeValueList,
            func_addTableData, func_onClose, func_onSubmit, func_handlerDel,
        };
    },
};
</script>
<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 150px;
}

.handle-input {
    width: 150px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>